import os
from datetime import timedelta

# 基础配置类
class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard-to-guess-string'
    SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://csss:12345@106.15.51.42/csss'
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    #    SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://remote_user:remote_password@remote_host:3306/remote_db'
    # 可以添加更多配置

    DEBUG = True
    TESTING = False
        

    DEBUG = False
    TESTING = True
    

    # 本地文件上传配置（作为备用）
    UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'app/static/uploads')
    MAX_CONTENT_LENGTH = 16 * 1024 * 1024  # 16MB
    
    # OSS对象存储配置
    USE_OSS_STORAGE = True  # 设置为True启用OSS存储，False使用本地存储
    OSS_ACCESS_KEY_ID = 'your_access_key_id'
    OSS_ACCESS_KEY_SECRET = 'your_access_key_secret'
    OSS_ENDPOINT = 'oss-cn-region.aliyuncs.com'  # OSS区域节点地址
    OSS_BUCKET_NAME = 'your-bucket-name'
    OSS_URL_PREFIX = 'https://your-bucket-name.oss-cn-region.aliyuncs.com/'  # 访问URL前缀

    @staticmethod
    def init_app(app):
        pass

# 开发环境配置
class DevelopmentConfig(Config):
    DEBUG = True
    # 使用与生产环境相同的数据库连接
    SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or 'mysql+pymysql://csss:12345@106.15.51.42/csss'
    
    @classmethod
    def init_app(cls, app):
        Config.init_app(app)
        # 开发环境特定的初始化

# 测试环境配置
class TestingConfig(Config):
    TESTING = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL') or 'mysql+pymysql://csss:12345@106.15.51.42/csss'
    
    @classmethod
    def init_app(cls, app):
        Config.init_app(app)
        # 测试环境特定的初始化

# 生产环境配置
class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'mysql+pymysql://csss:12345@106.15.51.42/csss'
    
    @classmethod
    def init_app(cls, app):
        Config.init_app(app)
        
        # 生产环境日志处理
        import logging
        from logging.handlers import RotatingFileHandler
        
        # 确保日志目录存在
        if not os.path.exists('logs'):
            os.mkdir('logs')
            
        file_handler = RotatingFileHandler('logs/box_admin.log', maxBytes=10240, backupCount=10)
        file_handler.setFormatter(logging.Formatter(
            '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
        ))
        file_handler.setLevel(logging.INFO)
        
        app.logger.addHandler(file_handler)
        app.logger.setLevel(logging.INFO)
        app.logger.info('Box Admin startup')

# 配置字典，用于通过环境变量选择配置
config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,
    'default': DevelopmentConfig
}
